package event;

import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片处理类
 */
public class FileProcessing {

    int i = -1;

    /**
     * 获取文件夹内所有图片，移动所有文件到新文件夹
     *
     * @param inputFileText 文件夹路径
     * @param outFileText   输出路径
     */
    public void removeFiles(String inputFileText, String outFileText) {
        System.out.println("处理点得到的图片文件夹路径" + inputFileText);
        System.out.println("处理点得到的输出路径" + outFileText);

        List<String> fileList = getAllFile(inputFileText, true);
//        System.out.println(fileList);

        for (String fileName : fileList) {
            fileName = fileName.replace("\\", "/");
            outFileText = outFileText.replace("\\", "/");

            String stringName = fileName.substring(fileName.lastIndexOf('/'));
//            System.out.println(stringName);


            File file = new File(fileName);
            File outFile = new File(outFileText + stringName);

            if (stringName.endsWith(".jpg")) {
                i = 0;
                if (file.renameTo(outFile)) {
                    System.out.println(fileName + "移动成功");
                } else {
                    System.out.println(fileName + "失败");
                }
            }


        }
        if (i == -1) {
            JOptionPane.showMessageDialog(null, "当前目录无.jpg文件");
        } else {
            JOptionPane.showMessageDialog(null, "迁移完成");
            File f = new File(inputFileText);
            if (f.delete()) {
                JOptionPane.showMessageDialog(null, "当前目录已删除");
            }
        }


    }

    /**
     * 获取路径下的所有文件/文件夹
     *
     * @param directoryPath  需要遍历的文件夹路径
     * @param isAddDirectory 是否将子文件夹的路径也添加到list集合中
     * @return List<String>
     */
    public List<String> getAllFile(String directoryPath, boolean isAddDirectory) {
        List<String> list = new ArrayList<String>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        if (files != null) {
            for (File file : files) {
//                判断是否是目录
                if (file.isDirectory()) {
//                    if(isAddDirectory){
////                        list.add(file.getAbsolutePath());
//                    }
//                    读取子文件夹种文件
                    list.addAll(getAllFile(file.getAbsolutePath(), isAddDirectory));
                } else {
                list.add(file.getAbsolutePath());
                }
            }
        }
        return list;
    }


}

//        JOptionPane.showMessageDialog(null,"迁移完成");
